package com.example.ktapplication.fragment

import android.os.Bundle
import androidx.fragment.app.Fragment
import com.example.ktapplication.R
import com.example.ktapplication.base.TabViewpagerFragment
import com.example.ktapplication.bean.SysBlogList
import com.google.gson.Gson

class SysTabFragment(private val data: String) : TabViewpagerFragment() {

    private val pages by lazy {
        mutableMapOf<String, Fragment>()
    }

    override fun onDate(savedInstanceState: Bundle?) {
    }

    override fun onViewBing() {
        super.onViewBing()
        binding.tabLayout.setBackgroundColor(resources.getColor(R.color.color_f2))
    }

    override fun loadData() {
        try {
            val sysBlogList = Gson().fromJson(data, SysBlogList::class.java)
            sysBlogList.let {
                it.data.children.forEach { item ->
                    pages[item.name] = BlogListFragment(item.id)
                }
                setupViewPager(pages.toList())
                binding.viewPager.currentItem = it.curIndex
            }
        } catch (e: Exception) {
            e.printStackTrace()
            showToast(e.message.toString())
            requireActivity().finish()
        }
    }

}